<?php

/**
 * @category controller
 * @name $Customers
 * @package front_end
 * @author HungPV <phamvanhung0818@gmail.com>
 */
class Customers extends MX_Controller {

    function __construct() {
        parent::__construct();
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $this->load->model('Email_model');
    }

    /**
     * Register Customer
     */
    function register() {
        $json = array(
            'err' => 1,
            'message' => 'no param posted'
        );
        if ($this->input->post('email')) {
            $email = $this->input->post('email');
            $phone = $this->input->post('phone');
            $name = $this->input->post('uname');
            $password = $this->input->post('pass');

            $customer = new Customer();
            $customer->email = $email;
            $customer->phone = $phone;
            $customer->fullname = $name;
            $customer->password = md5($password);
            $customer->status = 'not_actived';
            $customer->register_date = date('Y-m-d');
            $customer->active_code = $random_hash = md5(uniqid(rand(), true));

            if ($customer->save()) {
                $json = array(
                    'err' => 0,
                    'message' => 'Cảm ơn bạn đã đăng ký tài khoản trên IFree!',
                    'cid' => $customer->id,
                    'status' => $customer->status
                );
                $customer->send_otp_register($phone);
            } else {
                $json = array(
                    'err' => 1,
                    'message' => $customer->error
                );
            }
        }
        echo json_encode($json);
    }

    function valid_reg($email, $phone) {
        $customer = new Customer();
        $customer->valid_register(urldecode($email), $phone);
    }

    function get_register_code() {
        $phone = $this->input->post('phone');
        $code = $this->input->post('code');
        $email = $this->input->post('email');

        $customer = new Customer();
        $reg_code = $customer->get_otp_register($phone);

        $json = array();
        if ($reg_code['err'] == 0) {
            if ($code == $reg_code['code']) {
                $json = array(
                    'err' => 0,
                    'message' => 'exits'
                );

                //Send confirm email
                $customer = new Customer();
                $customer->where('email', $email)->get();
                if ($customer->result_count() > 0) {
                    $data['email'] = $customer->email;
                    $data['phone'] = $customer->phone;
                    $data['name'] = $customer->fullname;
                    $data['url'] = base_url() . 'customers/active/' . $customer->active_code;
                    $msg = $this->load->view('email_validate_register', $data, TRUE);
                    $this->Email_model->sendEmail($email, 'Xác thực email đăng ký - IFree', $msg);
                }
            } else {
                $json = array(
                    'err' => 1,
                    'message' => 'no_otp'
                );
            }
        } else {
            if ($reg_code['message'] == 'expired') {
                $json = array(
                    'err' => 1,
                    'message' => 'expired'
                );
            } else {
                $json = array(
                    'err' => 1,
                    'message' => 'no_otp'
                );
            }
        }
        echo json_encode($json);
    }

    function active($code = '') {
        $customer = new Customer();
        $customer->where('active_code', $code)->get();
        if ($customer->result_count() > 0) {
            if ($customer->status = Customer_status::Actived) {
                echo "Tài khoản đã được kích hoạt";
            } else {
                $customer->status = Customer_status::Actived;
                $customer->save();
                echo "Chúc mừng bạn đã trở thành thành viên của IFree!";
            }
        } else {
            echo "Xác nhận email thất bại";
        }
    }

}

?>